package net.wds.study.dep.controller;

import net.sf.json.JSONObject;
import net.wds.study.clazz.utils.PageModel;
import net.wds.study.dep.entity.Department;
import net.wds.study.dep.service.DepartmentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by gf on 2020/9/17.
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @RequestMapping(value = "/getPage", method = {RequestMethod.POST})
    public void getPage(@RequestParam Integer startIndex,
                        @RequestParam Integer pageSize,
                        @RequestParam String depName,
                        @RequestParam String depManger,
                        @RequestParam HttpServletResponse response) {
        PageModel<Department> departmentPageModel = departmentService.getPage(startIndex, pageSize, depName, depManger);

        try {
            response.getWriter().write(JSONObject.fromObject(departmentPageModel).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/saveDep", method = {RequestMethod.POST})
    public void saveDep(Department department, HttpServletResponse response) {
        boolean flag = departmentService.saveDep(department);
        try {
            if (flag) {
                response.getWriter().write("{ \"success\":true,\"id\":" + department.getId() + ",\"message\":\"保存成功\"}");
            } else {
                response.getWriter().write("{ \"success\":false,\"id\"null,\"message\"保存失败\"}");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping(value = "/updateDep", method = {RequestMethod.POST})
    public void updateDep(Department department, HttpServletResponse response) {
        boolean flag = departmentService.updateDep(department);
        try {
            if (flag) {
                response.getWriter().write("{ \"success\":true,\"id\":" + department.getId() + ",\"message\":\"修改成功\"}");
            } else {
                response.getWriter().write("{ \"success\":false,\"id\"null,\"message\"修改失败\"}");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping(value = "/deleteDep", method = {RequestMethod.POST})
    public void deleteDep(Integer id, HttpServletResponse response) {
        boolean flag = departmentService.deleteDep(id);
        try {
            if (flag) {
                response.getWriter().write("{ \"success\":true,\"id\":" + id + ",\"message\":\"删除成功\"}");
            } else {
                response.getWriter().write("{ \"success\":false,\"id\"null,\"message\"删除失败\"}");
                ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
